#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <windows.h>  // 用于设置控制台输出编码

// 生成连续的正弦信号
std::vector<double> generateSignal(double frequency, double duration, double samplingRate) {
    std::vector<double> signal;
    int numSamples = static_cast<int>(duration * samplingRate);
    for (int i = 0; i < numSamples; ++i) {
        double t = static_cast<double>(i) / samplingRate;
        // 使用 std::acos(-1) 替代 M_PI
        signal.push_back(std::sin(2 * std::acos(-1) * frequency * t));
    }
    return signal;
}

// 采样信号
std::vector<double> sampleSignal(const std::vector<double>& signal, double originalRate, double samplingRate) {
    std::vector<double> sampledSignal;
    int samplingInterval = static_cast<int>(originalRate / samplingRate);
    for (size_t i = 0; i < signal.size(); i += samplingInterval) {
        sampledSignal.push_back(signal[i]);
    }
    return sampledSignal;
}

// 线性插值重建信号
std::vector<double> reconstructSignal(const std::vector<double>& sampledSignal, double originalRate, double samplingRate) {
    std::vector<double> reconstructedSignal;
    int samplingInterval = static_cast<int>(originalRate / samplingRate);
    for (size_t i = 0; i < sampledSignal.size() - 1; ++i) {
        double start = sampledSignal[i];
        double end = sampledSignal[i + 1];
        for (int j = 0; j < samplingInterval; ++j) {
            double alpha = static_cast<double>(j) / samplingInterval;
            double value = (1 - alpha) * start + alpha * end;
            reconstructedSignal.push_back(value);
        }
    }
    return reconstructedSignal;
}

// 计算信号的均方误差
double calculateMSE(const std::vector<double>& original, const std::vector<double>& reconstructed) {
    double mse = 0.0;
    int minSize = std::min(original.size(), reconstructed.size());
    for (int i = 0; i < minSize; ++i) {
        double diff = original[i] - reconstructed[i];
        mse += diff * diff;
    }
    mse /= minSize;
    return mse;
}

int main() {
    SetConsoleOutputCP(65001);  // 设置控制台输出编码为 UTF-8

    double frequency = 10.0;  // 信号频率
    double duration = 1.0;    // 信号持续时间
    double originalRate = 1000.0;  // 原始采样率

    // 生成原始信号
    std::vector<double> originalSignal = generateSignal(frequency, duration, originalRate);

    // 大于两倍最高有效频率的采样率
    double highSamplingRate = 30.0;  // 大于 2 * 10 = 20
    std::vector<double> highSampledSignal = sampleSignal(originalSignal, originalRate, highSamplingRate);
    std::vector<double> highReconstructedSignal = reconstructSignal(highSampledSignal, originalRate, highSamplingRate);
    double highMSE = calculateMSE(originalSignal, highReconstructedSignal);

    // 小于两倍最高有效频率的采样率
    double lowSamplingRate = 15.0;  // 小于 2 * 10 = 20
    std::vector<double> lowSampledSignal = sampleSignal(originalSignal, originalRate, lowSamplingRate);
    std::vector<double> lowReconstructedSignal = reconstructSignal(lowSampledSignal, originalRate, lowSamplingRate);
    double lowMSE = calculateMSE(originalSignal, lowReconstructedSignal);

    std::cout << "大于两倍最高有效频率采样的均方误差: " << highMSE << std::endl;
    std::cout << "小于两倍最高有效频率采样的均方误差: " << lowMSE << std::endl;

    // 将结果写入文件以便可视化
    std::ofstream originalFile("original_signal.txt");
    for (double value : originalSignal) {
        originalFile << value << std::endl;
    }
    originalFile.close();

    std::ofstream highReconstructedFile("high_reconstructed_signal.txt");
    for (double value : highReconstructedSignal) {
        highReconstructedFile << value << std::endl;
    }
    highReconstructedFile.close();

    std::ofstream lowReconstructedFile("low_reconstructed_signal.txt");
    for (double value : lowReconstructedSignal) {
        lowReconstructedFile << value << std::endl;
    }
    lowReconstructedFile.close();

    system("pause");

    return 0;
}    